<?php
define('EmpireCMSAdmin', '1');
/**
 * 后台文件，生成页面
 * 365182575 cighsen02
 * 2021.09.19
 */
//设置执行是否超时，0为不设置超时
ini_set('max_execution_time', '0');

require dirname(__DIR__, 3) . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'connect.php';
require ECMS_PATH . 'e' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 'functions.php';
require ECMS_PATH .'e' . DIRECTORY_SEPARATOR . 'class' . DIRECTORY_SEPARATOR . 't_functions.php';
require ECMS_PATH .'e' . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'dbcache' . DIRECTORY_SEPARATOR . 'class.php';

$link = db_connect();
$empire = new mysqlquery();

$pwd = $_GET['pwd'] ?? null;

//验证用户
$lur = is_login();
$logininid  		= $lur['userid'];
$loginin 			= $lur['username'];
$loginrnd 			= $lur['rnd'];
$loginlevel 		= $lur['groupid'];
$loginadminstyleid 	= $lur['adminstyleid'];
//ehash
$ecms_hashur = hReturnEcmsHashStrAll();

$enews  = $_POST['enews'] ?? $_GET['enews'] ?? null;
$getType = $_GET['type'] ?? null;
$http	= ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$host 	= $_GET['host'] ?? $_SERVER['HTTP_HOST'];
$update = false;
$config = Config::twoir('config');

if (in_array($enews, ['sitemap', 'push'])) {
	$notice = '/**' . "\r\n";
    $notice .=' * 注意事项：' . "\r\n";
	$notice .=' * 1. 参数设置可以后台设置，不易容出错' . "\r\n";
	$notice .=' * 2. 对于熟悉的用户，可以直接修改' . "\r\n";
    $notice .=' * ' . "\r\n";
	$notice .=' * cighsen02 365182575 2021.11.11' . "\r\n";
	$notice .=' */' . "\r\n";
}

//设置
if ('sitemap' === $enews) {
	
	$tables = array_filter($_POST['table'] ?? [], function($item) use ($etable_r) {	return array_key_exists($item, $etable_r);});
	
	//值不同时更新，相同跳过
	if (isset($config['sitemap']['model']) && $tables !== $config['sitemap']['model']) {
		
		$config['sitemap']['model'] = $tables;
		$update = true;
	}
}
else {
	

	
}

if ($update && isset(CLASS_DIR['twoir'])) {

	$conf_file = dirname(CLASS_DIR['twoir']) . DIRECTORY_SEPARATOR . 'config.php';
	//写入文件
	if (file_exists($conf_file))
	{
		$str = "<?php" . "\r\n" . $ps . "return " . var_export($config, TRUE).";\r\n";
		WriteFiletext_n($conf_file, $str);
	}
	
	printerror('设置成功','map.php?enews=set' . hReturnEcmsHashStrHref2(0),1,0,1);
}


if(null !== $getType) {
    
    $type = ['xml', 'html', 'txt'];
    $page = $_GET['page'] ?: 'page';
    
    $map = new Twoir\SiteMap([], $host);
    
    if('all' === $getType) {
    	
    	foreach($type as $item) {
    		
    		$map->write('all', $item);
    	}
    	
    } elseif(in_array($getType, $type)) {
    	
    	$map->write('all', $getType);
    }
	
	printerror('生成成功','map.php' . hReturnEcmsHashStrHref2(1),1,0,1);
}

//缓存
db_close();
$empire=null;
?>
<!DOCTYPE html>
<html>
<head>
	<link href="../../adminstyle/1/adminstyle.css" rel="stylesheet" type="text/css">
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title>sitemap更新，xml、txt、html格式</title>
</head>
<?php 
if('set' === $enews || 'sitemap' === $enews):
	extract($config['sitemap']);
?>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
	<td>位置：sitemap管理&gt; <a href="map.php?enews=set<?=$ecms_hashur['ehref']?>">sitemap设置</a></td>
	<td width="50%"></td>
</tr>
</table>
<br>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
	<tbody>
        <form name="form2" method="post" action="map.php">
        <input name="enews" type="hidden" value="sitemap">
		<?=$ecms_hashur['form']?>
		
		<tr class="header">
			<td width="12%" height="25"><div align="center">参数名</div></td>
			<td width="58%" height="25"><div align="center">选择开启数据表</div></td>
			<td width="30%" height="25"><div align="center">说明</div></td>
		</tr>
		
		<tr bgcolor="#FFFFFF" onmouseout="this.style.backgroundColor='#ffffff'" onmouseover="this.style.backgroundColor='#C3EFFF'" style="background-color: rgb(255, 255, 255);">
			<td height="25"><div align="right"> 设置要启用的数据表: </div></td>
			<td height="25">

				<?php foreach($etable_r as $table => $value):?>

				<input type="checkbox" name="table[]" value="<?php echo $table;?>"<?php echo in_array($table, $model) ? ' checked' : '';?>> <?php echo $table;?>

				<?php endforeach;?>
			</td>
			<td height="25">选择要开启数据表, 选中时为开启状态，灰色为内部表，禁止开启</td>
		</tr>

        <tr bgcolor="#FFFFFF">
        	<td height="25"></td>
        	<td height="25">
        		<div align="center"><input type="submit" name="Submit1" value="修改">&nbsp;</div>
        	</td>
        	<td height="25"></td>
        </tr>

        </form>
	</tbody>
</table>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
	<tbody>
		<tr>
			<td height="25"><font color="#666666">说明：选中时为开启状态。</font></td>
		</tr>
	</tbody>
</table>
<?php else:?>

<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1">
<tr>
	<td>位置：sitemap管理&gt; <a href="map.php<?php echo $ecms_hashur['whhref'];?>">sitemap生成</a></td>
	<td width="50%">
		<div align="right" class="emenubutton">
			<input type="button" name="Submit5" value="sitemap设置"	onclick="self.location.href='map.php?enews=set<?=$ecms_hashur['ehref']?>';">
		</div>
	</td>
</tr>
</table>
<br>
<table width="100%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">
	<tbody>
		<tr class="header">
			<td width="12%" height="25"><div align="center">生成时间</div></td>
			<td width="58%" height="25"><div align="center">文件</div></td>
			<td width="30%" height="25"><div align="center">说明</div></td>
		</tr>
		<?php
		if(!empty($list = glob(ECMS_PATH . 'sitemap' . DIRECTORY_SEPARATOR . $host . '.*'))):
		foreach($list as $file):
			$url = $http . $host . str_replace(ECMS_PATH, '/', $file);
		?>
        <tr bgcolor="#FFFFFF" onmouseout="this.style.backgroundColor='#ffffff'" onmouseover="this.style.backgroundColor='#C3EFFF'" style="background-color: rgb(255, 255, 255);">
        	<td height="25">
				<div align="center">
					<?php echo date('Y-m-d H:i:s', filectime($file));?>
				</div>
			</td>
        	<td height="25">
				<a href="<?php echo $url;?>" target="_blank"><?php echo $url;?></a>
        	</td>
        	<td height="25">
				
			</td>
        </tr>
		<?php endforeach;?>
		<?php endif;?>
		
		<tr bgcolor="#FFFFFF">
			<td height="25"><div align="right"> 操作	</div></td>
			<td height="25">
				<div align="center">
					<a href="map.php?type=xml<?php echo $ecms_hashur['ehref'];?>">更新xml</a>&nbsp;
					<a href="map.php?type=html<?php echo $ecms_hashur['ehref'];?>">更新html</a>&nbsp;
					<a href="map.php?type=txt<?php echo $ecms_hashur['ehref'];?>">更新txt</a>&nbsp;
					<a href="map.php?type=all<?php echo $ecms_hashur['ehref'];?>">更新xml,html,txt</a>
				</div>
			</td>
			<td height="25"></td>
		</tr>

	</tbody>
</table>
<table width="100%" border="0" cellspacing="1" cellpadding="3">
	<tbody>
		<tr>
			<td height="25"><font color="#666666">说明：xml可以定期更新。</font></td>
		</tr>
	</tbody>
</table>

<?php endif;?>
</body>
</html>